#tunnel.py
# Written by koya
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

import maths
import math        

class tunnel_obj( object ):

    def __init__( self ):

        self.__tunnels = []
        for i in xrange( 416 ):
            self.__tunnels.append( maths.vector_obj(0.0,0.0,0.0) )
            
        self.__TEXTURE_SPEED = float(1.0/50.0)
        self.__t_angle = 0.0
        self.__t_speed = 0.3
         

    def get_tunnel_info( self, i, j ):

        return self.__tunnels[ i * 32 + j]

    def render( self, texid ):

        j1 = 0.0
        j2 = 0.0
        color = 0.0
        
        glEnable(GL_TEXTURE_2D)
        glBindTexture(GL_TEXTURE_2D, texid) 
        
        glPushMatrix()

        #glRotatef( -15.0, 0, 1, 0 )
        glTranslatef( 0.0, 0.0, 2.5 )
             
        self.__t_angle = self.__t_angle + self.__t_speed
        
        
        angle1 = self.__t_angle * 0.030303   #1/33 == 0.030303

        for i  in xrange( 13 ):

            angle0 = 2 * math.pi/12.0 * i
            for j in xrange( 32 ):

                angle = self.__t_angle + 2 * j         
                angle2 = angle*0.030303
                radian =  3 - j*0.0833333

                index = i * 32 + j

                # 1/29.0 = 0.0344828, 1/13.0 = 0.0769231
       
                self.__tunnels[index].x =radian * math.cos( angle0 ) + 2 * math.sin(angle * 0.0344828) +math.cos(angle * 0.0769231 ) -\
                                      2 * math.sin( self.__t_angle * 0.0344828 ) - math.cos( self.__t_angle * 0.0769231)
                self.__tunnels[index].y =radian * math.sin( angle0 ) + 2 * math.cos(angle2) + math.sin(angle2) -\
                                      2 * math.cos( angle1 ) - math.sin( angle1 )
                self.__tunnels[index].z = -j 
        
        #draw tunnel
        #glPolygonMode( GL_FRONT_AND_BACK, GL_LINE )

        glBegin(GL_QUADS)
        for j in xrange( 31 ):

            j1 = j*0.03125 + self.__t_angle * self.__TEXTURE_SPEED # 1/32.0 = 0.03125
            j2 = (j+1)*0.03125 + self.__t_angle * self.__TEXTURE_SPEED
            #near the end of the tunnel, fade the effect away

            if j > 24:
                color = float(1.0-(j-24)*0.1)
            else:
                color = 1.0
                
            glColor4f(color, color, color, 1)
            
            for i in xrange( 12):

                div_i0 = (i-3)*0.083
                div_i1 = (i-2)*0.083  #1/12.0 = 0.0833333333

                index_i0 = i * 32 + j
                index_i1 = (i+1)*32 + j
                index_j0 = index_i0 + 1
                index_j1 = index_i1 + 1

                glTexCoord2f(div_i0, j1)
                glVertex3f(self.__tunnels[ index_i0 ].x, self.__tunnels[index_i0].y, self.__tunnels[index_i0].z)
                glTexCoord2f(div_i1, j1)
                glVertex3f(self.__tunnels[ index_i1].x, self.__tunnels[index_i1].y, self.__tunnels[index_i1].z)
                glTexCoord2f(div_i1, j2)
                glVertex3f(self.__tunnels[index_j1].x, self.__tunnels[index_j1].y, self.__tunnels[index_j1].z)
                glTexCoord2f(div_i0, j2)
                glVertex3f(self.__tunnels[index_j0].x, self.__tunnels[index_j0].y, self.__tunnels[index_j0].z)        
        glEnd()

        #glPolygonMode( GL_FRONT_AND_BACK, GL_FILL )


        glPopMatrix()
        glDisable(GL_TEXTURE_2D)
        glBindTexture( GL_TEXTURE_2D, 0)
        #glDeleteTextures(texid)
        #return True
    

        
       
